package com.zb.uniman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zb.uniman.mapper.SysUserMapper;
import com.zb.uniman.model.LoginUser;
import com.zb.uniman.model.SysUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

//真正花费数据库的东西
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserMapper userMapper;


//这个只是众多步骤中的一个
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

            //查询用户信息
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUser::getUserName,username);
            SysUser user = userMapper.selectOne(queryWrapper);
            //如果没有查询到用户就抛出异常
            if(Objects.isNull(user)){
                throw new RuntimeException("用户名或者密码错误");
            }
//权限查询
            List<String> list = new ArrayList<>(Arrays.asList("test","admin"));
//        List<String> list = menuMapper.selectPermsByUserId(user.getId());
            //把数据封装成UserDetails返回 。有了test ，admin权限
            return new LoginUser(user,list);
//        return new LoginUser(user);
    }
}
